package com.qingcloud.adminbackend.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingcloud.adminbackend.anno.ProductApi;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.base.ApiTreeNode;
import com.qingcloud.adminbackend.entity.product.SysProductCommand;
import com.qingcloud.adminbackend.entity.product.SysProductCommandDto;
import com.qingcloud.adminbackend.entity.product.SysProductDecoder;
import com.qingcloud.adminbackend.exception.WmCommonException;
import com.qingcloud.adminbackend.pojo.IdListParam;
import com.qingcloud.adminbackend.pojo.dto.sysproduct.*;
import com.qingcloud.adminbackend.pojo.vo.sysproduct.*;
import com.qingcloud.adminbackend.pojo.product.CommandResultDTO;
import com.qingcloud.adminbackend.pojo.product.ComponentParam;
import com.qingcloud.adminbackend.pojo.product.ProductCommandFieldDTO;
import com.qingcloud.adminbackend.service.ISysProductTopicService;
import com.qingcloud.adminbackend.service.ISysProductTypeService;
import com.qingcloud.adminbackend.service.impl.ISysProductCommandClassifyService;
import com.qingcloud.adminbackend.utils.CookieUtil;
import com.qingcloud.adminbackend.utils.ImportFileUtil;
import com.qingcloud.base.pojo.PageResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 设备产品型号
 *
 * @author yangfan
 * @since 2021/11/04
 */
@RestController
@RequestMapping("/sysProduct")
public class SysProductController {

    @Resource
    private ISysProductTypeService sysProductTypeService;

    @Resource
    private ISysProductTopicService iSysProductTopicService;

    public SysProductController() {
    }

    @ApiOperation(value = "新增产品信息")
    @PostMapping("/addProduct")
    public AjaxResult<Long> addProduct(@Validated @RequestBody SysProductAddDTO sysProductAddDTO, HttpServletRequest request) {
        return AjaxResult.ok(sysProductTypeService.addProduct(sysProductAddDTO, CookieUtil.getUserId(request)));
    }

    @ApiOperation(value = "修改产品信息")
    @PostMapping("/modProduct")
    public AjaxResult<Long> modProduct(@Validated @RequestBody SysProductModDTO sysProductModDTO, HttpServletRequest request) {
        return AjaxResult.ok(sysProductTypeService.modProduct(sysProductModDTO, CookieUtil.getUserId(request)));
    }

    @ApiOperation(value = "删除产品信息", notes = "删除产品信息")
    @PostMapping("/deleteProducts")
    public AjaxResult<?> deleteProduct(@ApiParam(required = true, value = "产品id集合") @RequestBody List<Integer> productTypeIdList) {
        sysProductTypeService.deleteProductList(productTypeIdList);
        return AjaxResult.ok();
    }

    @ApiOperation(value = "分页产品列表")
    @GetMapping("/pageProductList")
    public AjaxResult<Page<PageProductListVO>> pageProductList(@ApiParam(required = true, value = "分页产品列表请求DTO") PageProductListDTO pageProductListDTO) {
        return AjaxResult.ok(sysProductTypeService.pageProductList(pageProductListDTO));
    }

    @GetMapping("/productDetail/{productTypeId}")
    @ApiOperation(value = "产品详情")
    public AjaxResult<ProductDetailVO> productDetail(@ApiParam(required = true, value = "产品id") @PathVariable Integer productTypeId) {
        return AjaxResult.ok(sysProductTypeService.productDetail(productTypeId));
    }

    @ProductApi
    @ApiOperation(value = "分页查询解码器组件")
    @PostMapping("/decoder/components")
    public AjaxResult<PageResult<SysProductDecoder>> pageComponentDecoders(@RequestBody ComponentParam componentParam) {
        return AjaxResult.ok(sysProductTypeService.pageComponentDecoders(componentParam));
    }

    @ProductApi
    @ApiOperation(value = "关联解码器组件")
    @PostMapping("/decoder/relation")
    public AjaxResult<Integer> addOrModProductDecoder(@RequestBody List<SysProductDecoder> decoders) {
        return AjaxResult.ok(sysProductTypeService.addOrModProductDecoder(decoders));
    }

    @ProductApi
    @PostMapping("/decoder/page")
    @ApiOperation(value = "产品数据模型分页查询")
    public AjaxResult pageDecoder(@RequestBody ComponentParam componentParam) {
        return AjaxResult.ok(sysProductTypeService.productDecoderPage(componentParam));
    }

    @ProductApi
    @PostMapping("/decoder/delete")
    @ApiOperation(value = "删除产品数据模型")
    public AjaxResult deleteDecoder(@RequestBody IdListParam param) {
        return AjaxResult.ok(sysProductTypeService.deleteDecoder(param));
    }

    @ProductApi
    @ApiOperation(value = "保存/修改产品指令模型", notes = "保存/修改产品指令模型", response = SysProductCommandDto.class)
    @PostMapping("/saveOrModProductCommand")
    public AjaxResult<SysProductCommand> saveProductCommand(@Validated @RequestBody SysProductCommandDto sysProductCommandDto, HttpServletRequest request) {
        return AjaxResult.ok(sysProductTypeService.saveOrModProductCommand(sysProductCommandDto, CookieUtil.getUserId(request)));
    }

    @ProductApi
    @ApiOperation(value = "导入产品指令", notes = "导入产品指令")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classifyId", value = "指令分类ID", dataTypeClass = Integer.class, paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "productTypeId", value = "产品类型ID", dataTypeClass = Integer.class, paramType = "query", defaultValue = "1")
    })
    @PostMapping("/command/import")
    public AjaxResult<SysProductCommand> importProductCommand(@RequestParam("file") MultipartFile file,
                                                              @RequestParam("classifyId") Integer classifyId,
                                                              @RequestParam("productTypeId") Integer productTypeId,
                                                              HttpServletRequest request) throws IOException {
        //导入文件解析
        final List<SysProductCommandDto> dtoList = ImportFileUtil.productCommandImportParse(file.getInputStream(), classifyId, productTypeId);
        if (CollUtil.isEmpty(dtoList)) {
            throw new WmCommonException("上传excel文件内容是空的");
        }
        for (SysProductCommandDto dto : dtoList) {
            sysProductTypeService.saveOrModProductCommand(dto, CookieUtil.getUserId(request));
        }
        return AjaxResult.ok();
    }

    @ProductApi
    @ApiOperation(value = "删除产品指令信息", notes = "删除产品指令信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productCommandId", value = "产品指令id", dataType = "Long", paramType = "query", defaultValue = "1")
    })
    @PostMapping("/deleteProductCommand")
    public AjaxResult<Integer> deleteProductCommand(Integer productCommandId) {
        return AjaxResult.ok(sysProductTypeService.deleteProductCommand(productCommandId));
    }

    @ProductApi
    @ApiOperation(value = "查询产品指令信息")
    @GetMapping("/commandByProdutId/{productCommandId}")
    public AjaxResult<SysProductCommandVO> commandByProdutId(@PathVariable(value = "productCommandId") Integer productCommandId) {
        return AjaxResult.ok(sysProductTypeService.queryCommandInfo(productCommandId));
    }

    @ProductApi
    @ApiOperation(value = "保存指令源码信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commandId", value = "指令id", dataType = "int", paramType = "path", defaultValue = "1")
    })
    @PostMapping("/command/source/save/{commandId}")
    public AjaxResult<String> saveCommandSource(@PathVariable(value = "commandId") Integer commandId) {
        return AjaxResult.ok(sysProductTypeService.saveCommandSource(commandId));
    }

    @ProductApi
    @ApiOperation(value = "查询指令字段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commandId", value = "指令id", dataType = "int", paramType = "path", defaultValue = "1")
    })
    @GetMapping("/command/field/{commandId}")
    public AjaxResult<ProductCommandFieldDTO> findCommandField(@PathVariable(value = "commandId") Integer commandId) {
        return AjaxResult.ok(sysProductTypeService.findCommandField(commandId));
    }

    @ProductApi
    @ApiOperation(value = "指令数据预览")
    @PostMapping("/command/presend")
    public AjaxResult<String> getCommandContent(@RequestBody JSONObject jsonObject) {
        return AjaxResult.ok(sysProductTypeService.getCommandContent(jsonObject));
    }

    @ProductApi
    @ApiOperation(value = "指令数据发送")
    @PostMapping("/command/send")
    public AjaxResult<CommandResultDTO> sendCommand(@RequestBody JSONObject jsonObject) {
        return AjaxResult.ok(sysProductTypeService.sendCommand(jsonObject));
    }

    /**
     * 通过id查询产品主题
     */
    @GetMapping("/getSysProductTopicById/{id}")
    @ApiOperation(value = "通过id查询产品主题")
    public AjaxResult<SysProductTopicDetailVO> selectSysProductTopicDetailById(@PathVariable(value = "id") Integer id) {
        return AjaxResult.ok(iSysProductTopicService.selectSysProductTopicDetailById(id));
    }

    /**
     * 新增产品主题
     */
    @PostMapping("/addSysProductTopic")
    @ApiOperation(value = "新增产品主题")
    public AjaxResult<Integer> addSysProductTopic(@RequestBody @Validated AddSysProductTopicDTO dto, HttpServletRequest request) {
        return AjaxResult.ok(iSysProductTopicService.addSysProductTopic(dto, CookieUtil.getUserId(request)));
    }

    /**
     * 删除产品主题
     */
    @PostMapping("/deleteSysProductTopicByIds")
    @ApiOperation(value = "删除产品主题")
    public AjaxResult<Boolean> deleteSysProductTopic(@RequestBody List<Integer> idList) {
        return AjaxResult.ok(iSysProductTopicService.deleteSysProductTopicList(idList));
    }

    /**
     * 通过id更新产品主题
     */
    @PutMapping("/updateSysProductTopicById")
    @ApiOperation(value = "通过id更新产品主题")
    public AjaxResult<Boolean> updateSysProductTopicById(@RequestBody @Validated UpdateSysProductTopicDTO dto, HttpServletRequest request) {
        return AjaxResult.ok(iSysProductTopicService.updateSysProductTopicById(dto, CookieUtil.getUserId(request)));
    }

    /**
     * 分页查询产品主题
     */
    @GetMapping("/pageSysProductTopicList")
    @ApiOperation(value = "分页查询产品主题")
    public AjaxResult<IPage<PageSysProductTopicListVO>> pageSysProductTopicList(@ApiParam(required = true, value = "分页查询产品主题请求DTO") PageSysProductTopicListDTO pageParam) {
        return AjaxResult.ok(iSysProductTopicService.pageSysProductTopicList(pageParam));
    }

    @Resource
    private ISysProductCommandClassifyService iSysProductCommandClassifyService;


    /**
     * 新增指令分类
     */
    @PostMapping("/addSysProductCommandClassify")
    @ApiOperation(value = "新增指令分类")
    public AjaxResult<Integer> addSysProductCommandClassify(@Validated @RequestBody AddSysProductCommandClassifyDTO dto, HttpServletRequest request) {
        return AjaxResult.ok(iSysProductCommandClassifyService.addSysProductCommandClassify(dto, CookieUtil.getUserId(request)));
    }

    /**
     * 通过id删除
     */
    @PostMapping("/deleteSysProductCommandClassifyByIds")
    @ApiOperation(value = "通过id删除指令分类")
    public AjaxResult<Boolean> deleteSysProductCommandClassify(@RequestBody List<Integer> idList) {
        return AjaxResult.ok(iSysProductCommandClassifyService.deleteSysProductCommandClassifyList(idList));
    }

    /**
     * 通过id更新数据
     */
    @PutMapping("/updateSysProductCommandClassifyById")
    @ApiOperation(value = "通过id更新指令分类")
    public AjaxResult<Boolean> updateSysProductCommandClassifyById(@Validated @RequestBody UpdateSysProductCommandClassifyDTO dto, HttpServletRequest request) {
        return AjaxResult.ok(iSysProductCommandClassifyService.updateSysProductCommandClassifyById(dto, CookieUtil.getUserId(request)));
    }

    /**
     * 产品指令分类树结构
     */
    @GetMapping("/productCommandClassifyTree/{productTypeId}")
    @ApiOperation(value = "产品指令分类树结构")
    public AjaxResult<List<ApiTreeNode>> productCommandClassifyTree(@PathVariable(value = "productTypeId") Integer productTypeId) {
        return AjaxResult.ok(iSysProductCommandClassifyService.productCommandClassifyTree(productTypeId));
    }
}
